listenResources

fun listenResources(link: String, userAgent: String, headers: Map<String, String>, timeout: Long, executeOnFinish: String? = null, filter: (String?, Map<String, String>?) -> Boolean, callback: (String?, Map<String, String>) -> Unit)(source)

Listen for network resources while loading a page and return the first one that matches the filter.

This function loads the specified link in a WebView and intercepts network requests. It uses the provided filter to identify a target resource. Once the page loading is finished (or the timeout is reached), the callback is invoked with the URL and headers of the first matched resource.

Parameters

link

The URL to load in the WebView

userAgent

The user agent string to use for the request

headers

A map of additional headers to include in the main request.

timeout

The maximum time in milliseconds to wait after the page has finished loading before invoking the callback. This ensures that any delayed or asynchronous resource requests are captured

executeOnFinish

Optional JavaScript code to execute after the page has finished loading (javascript:<code>)

filter

A lambda function that takes a resource URL (as a String?) and returns true if it's the desired resource, false otherwise

callback

A lambda function to be invoked with the URL and headers of the matched resource. It receives the resource URL (String?) and its response headers (Map<String, String>). If no resource matches the filter before the timeout, null and an empty map are returned.